package com.ruoyi.wms.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.utils.MapstructUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.ruoyi.wms.domain.bo.stock.PurchaseRequestDetailsBo;
import com.ruoyi.wms.domain.entity.stock.PurchaseRequestDetails;
import com.ruoyi.wms.domain.vo.ItemSkuVo;
import com.ruoyi.wms.domain.vo.PurchaseRequestDetailsVo;
import com.ruoyi.wms.mapper.PurchaseRequestDetailsMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

import static com.baomidou.mybatisplus.extension.toolkit.Db.saveOrUpdateBatch;

/**
 * 采购申请详情Service业务层处理
 *
 * @author 邓滔滔
 * @date 2024-11-20
 */
@RequiredArgsConstructor
@Service
public class PurchaseRequestDetailsService {

    private final PurchaseRequestDetailsMapper purchaseRequestDetailsMapper;
    private final ItemSkuService itemSkuService;
    /**
     * 查询采购申请详情
     */
    public PurchaseRequestDetailsVo queryById(Long id){
        return purchaseRequestDetailsMapper.selectVoById(id);
    }

    /**
     * 查询采购申请详情列表
     */
    public TableDataInfo<PurchaseRequestDetailsVo> queryPageList(PurchaseRequestDetailsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PurchaseRequestDetails> lqw = buildQueryWrapper(bo);
        Page<PurchaseRequestDetailsVo> result = purchaseRequestDetailsMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询采购申请详情列表
     */
    public List<PurchaseRequestDetailsVo> queryList(PurchaseRequestDetailsBo bo) {
        LambdaQueryWrapper<PurchaseRequestDetails> lqw = buildQueryWrapper(bo);
        return purchaseRequestDetailsMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PurchaseRequestDetails> buildQueryWrapper(PurchaseRequestDetailsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PurchaseRequestDetails> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getRequestId() != null, PurchaseRequestDetails::getRequestId, bo.getRequestId());
        lqw.eq(bo.getSkuId() != null, PurchaseRequestDetails::getSkuId, bo.getSkuId());
        lqw.eq(bo.getQuantity() != null, PurchaseRequestDetails::getQuantity, bo.getQuantity());
        lqw.eq(bo.getPrice() != null, PurchaseRequestDetails::getPrice, bo.getPrice());
        lqw.eq(bo.getAmount() != null, PurchaseRequestDetails::getAmount, bo.getAmount());
        return lqw;
    }

    /**
     * 新增采购申请详情
     */
    public void insertByBo(PurchaseRequestDetailsBo bo) {
        PurchaseRequestDetails add = MapstructUtils.convert(bo, PurchaseRequestDetails.class);
        purchaseRequestDetailsMapper.insert(add);
    }

    /**
     * 修改采购申请详情
     */
    public void updateByBo(PurchaseRequestDetailsBo bo) {
        PurchaseRequestDetails update = MapstructUtils.convert(bo, PurchaseRequestDetails.class);
        purchaseRequestDetailsMapper.updateById(update);
    }

    /**
     * 批量删除采购申请详情
     */
    public void deleteByIds(Collection<Long> ids) {
        purchaseRequestDetailsMapper.deleteBatchIds(ids);
    }

    /**
     * 构造采购申请详情
     * @param purchaseId
     * @return
     */
    public List<PurchaseRequestDetailsVo> queryByPurchaseId(Long purchaseId) {
        PurchaseRequestDetailsBo bo=new PurchaseRequestDetailsBo();
        bo.setRequestId(purchaseId);
        List<PurchaseRequestDetailsVo> details=queryList(bo);
        if (CollUtil.isEmpty(details)){
            return Collections.emptyList();
        }
        Set<Long> skuIds = details
            .stream()
            .map(PurchaseRequestDetailsVo::getSkuId)
            .collect(Collectors.toSet());
        Map<Long, ItemSkuVo> itemSkuMap = itemSkuService.queryVosByIds(skuIds)
            .stream()
            .collect(Collectors.toMap(ItemSkuVo::getId, Function.identity()));
        details.forEach(item->item.setItemSku(itemSkuMap.get(item.getSkuId())));
        return details;
    }

    /**
     * 保存采购申请详情
     * @param list
     */
    @Transactional
    public void saveDetails(List<PurchaseRequestDetails> list) {
        if (CollUtil.isEmpty(list)){
            return;
        }
        saveOrUpdateBatch(list);
    }
}
